import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/pay_model/bank_card_list_model.dart';
import 'package:yysk_app/pages/nft_pages/pay_pages/pay_order/pay_order_controller.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/manage_bank/add_bank_card_page.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:oktoast/oktoast.dart';
import 'select_bank_controller.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/withdraw/withdraw_controller.dart';

/// 银行卡选择弹窗
Future<BankCardListModel?> showSelectBankDialog({
  required BuildContext context,
}) async {
  return await showDialog<BankCardListModel>(
    context: context,
    barrierDismissible: false,
    useSafeArea: false,
    builder: (context) => const SelectBankDialog(),
  );
}

class SelectBankDialog extends StatelessWidget {
  const SelectBankDialog({super.key});
  @override
  Widget build(BuildContext context) {
    final double bottom = MediaQuery.of(context).viewInsets.bottom;
   WithdrawController withdrawController = Get.find();
    return Material(
      color: Colors.black.withOpacity(0.3),
      child: Stack(
        children: [

          Align(
            alignment: Alignment.bottomCenter,
            child: ClipRRect(
              borderRadius: const BorderRadius.vertical(
                top: Radius.circular(32),
              ),
              child: Container(
                width: double.infinity,
                height: MediaQuery.of(context).size.height * 0.6 + bottom,
                color: Colors.white,
                padding: EdgeInsets.only(
                  left: 0,
                  right: 0,
                  top: 32,
                  bottom: bottom > 0 ? bottom : 32,
                ),
                child: Stack(
                  children: [
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        const SizedBox(height: 8),
                        const Text(
                          '选择提现银行卡',
                          style: TextStyle(
                            fontSize: 22,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        const SizedBox(height: 24),
                        Obx(
                          () => Column(
                            children: [
                              ...withdrawController.payCards.map(
                                (bank) => _buildBankItem(bank, context),
                              ),
                              _buildAddBankItem(context),
                            ],
                          ),
                        ),
                        const SizedBox(height: 16),
                      ],
                    ),
                    // 右上角关闭按钮
                    Positioned(
                      right: 16,
                      top: 0,
                      child: GestureDetector(
                        onTap: () => Navigator.of(context).pop(),
                        child: Container(
                          width: 40,
                          height: 40,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            shape: BoxShape.circle,
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black.withOpacity(0.08),
                                blurRadius: 8,
                              ),
                            ],
                          ),
                          child: const Icon(Icons.close, size: 24),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildBankItem(BankCardListModel bank, BuildContext context) {
    return Column(
      children: [
        ListTile(
          title: Text(
            '${bank.bankName}（${bank.bankCardNo}）',
            style: const TextStyle(fontSize: 18, fontWeight: FontWeight.normal),
          ),
          trailing: const Icon(
            Icons.arrow_forward_ios,
            size: 18,
            color: Colors.black26,
          ),
          contentPadding: const EdgeInsets.symmetric(horizontal: 24),
          onTap: () {
            WithdrawController withdrawController = Get.find();
            withdrawController.selsectInfo.value = bank;
           Navigator.of(context).pop();
          },
          minVerticalPadding: 18,
        ),
        const Divider(
          height: 1,
          thickness: 1,
          indent: 24,
          endIndent: 24,
          color: Color(0xFFF2F2F2),
        ),
      ],
    );
  }

  Widget _buildAddBankItem(BuildContext context) {
    return ListTile(
      leading: ImageAssetWidget(imageUrl: 'static/images/wallet/bank_info.png', width: 28,
        height: 28,)
      // Image.asset(
      //   'static/images/wallet/add_bank.png',
      //   width: 28,
      //   height: 28,
      // )
      ,
      title: const Text(
        '修改银行卡',
        style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal),
      ),
      trailing: const Icon(
        Icons.arrow_forward_ios,
        size: 18,
        color: Colors.black26,
      ),
      contentPadding: const EdgeInsets.symmetric(horizontal: 24),
      onTap: () {
        Get.back();
          WithdrawController withdrawController = Get.find();
             if (withdrawController.type.value == '1') {
        Get.toNamed(
          '${AddBankCardPage.path}?comeFrom=add',
          arguments: {
            'comeFrom': 'add',
            'titleText':((withdrawController.payCards.length == 1)
                    ? '修改结算卡'
                    : '添加结算卡')
          },
        );
      }else if(withdrawController.type.value == '2'){
       Get.toNamed(
          '${AddBankCardPage.path}?comeFrom=add',
          arguments: {
            'comeFrom': 'add',
            'plat':'ENTERPRISE',
            'titleText':((withdrawController.payCards.length == 1)
                    ? '修改结算卡'
                    : '添加结算卡')
                    
          },
        );
      }else{
 Get.toNamed(
          '${AddBankCardPage.path}?comeFrom=add',
          arguments: {
            'comeFrom': 'add',
            'plat':'INDIVIDUAL_BUSINESS',
            'titleText':((withdrawController.payCards.length == 1)
                    ? '修改结算卡'
                    : '添加结算卡')
                    
          },
        );
      }
      //   Get.toNamed(
      // '${AddBankCardPage.path}?bindType=WITHDRAW&comeFrom=Withdrawal',
      // arguments: {
      //   'bindType': 'WITHDRAW',
      //   'comeFrom':'Withdrawal'
      // },);
      },
      minVerticalPadding: 18,
    );
  }
}
